#include "GameScene.h"
#include <QDebug>
#include <QKeyEvent>
#include <QApplication>

GameScene::GameScene():
    m_keyPressed (Qt::Key_unknown)
{
}

void GameScene::keyPressEvent(QKeyEvent *keyEvent)
{
    if ( keyEvent->isAutoRepeat() )
    {
        keyEvent->ignore();
        return;
    }

    int key = keyEvent->key();
    if (key == Qt::Key_Space)
    {
        qDebug() << "start shooting";
        //start fire
    }
    else if ( m_keyPressed != Qt::Key_unknown )
    {
        keyEvent->ignore();
        return;
    }
    else if (key == Qt::Key_Up)
    {
        m_keyPressed = key;
        qDebug() << "start moving up";
        //turn to north and start move
    }
    else if (key == Qt::Key_Down)
    {
        m_keyPressed = key;
        qDebug() << "start moving down";
        //turn to south and start move
    }
    else if (key == Qt::Key_Right)
    {
        m_keyPressed = key;
        qDebug() << "start moving right";
        //turn to east and start move
    }
    else if (key == Qt::Key_Left)
    {
        m_keyPressed = key;
        qDebug() << "start moving left";
        //turn to west and start move
    }
}

void GameScene::keyReleaseEvent(QKeyEvent *keyEvent)
{
    if ( keyEvent->isAutoRepeat() )
    {
        keyEvent->ignore();
        return;
    }

    int key = keyEvent->key();
    if (key == Qt::Key_Space)
    {
        qDebug() << "stop shooting";
        //stop shooting
    }
    else if( keyEvent->key() == m_keyPressed)
    {
       // if (key == Qt::Key_Up
       //         || key == Qt::Key_Down
       //         || key == Qt::Key_Left
       //         || key == Qt::Key_Right)
        {
            m_keyPressed = Qt::Key_unknown;
            qDebug() << "stop moving" << key;
            //stop moving
        }
    }
}
